package com.insightg3.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;

public class AdvanceSearchActivity extends Activity {
	private EditText txtKeyword;
	private EditText startPrice;
	private EditText endPrice;
	/** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.main_advance);
    	Spinner spinner = (Spinner)findViewById(R.id.cmdAreaAdvance);
    	ArrayAdapter adapter = ArrayAdapter.createFromResource(
    			this, R.array.value_where, android.R.layout.simple_spinner_item);
    	adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	spinner.setAdapter(adapter);
    	Spinner spinner2 = (Spinner)findViewById(R.id.Spinner02);
    	ArrayAdapter adapter2 = ArrayAdapter.createFromResource(
    			this, R.array.value_order, android.R.layout.simple_spinner_item);
    	adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	spinner2.setAdapter(adapter2);
    	
    	//initialization
		txtKeyword =  (EditText)findViewById(R.id.EditText01);
		startPrice = (EditText)findViewById(R.id.EditText02);
		endPrice = (EditText)findViewById(R.id.EditText03);
		((Button) findViewById(R.id.Button01)).setOnClickListener(mSearchListener);
		((Button) findViewById(R.id.Button02)).setOnClickListener(mClearListener);
    }
    
	OnClickListener mSearchListener = new OnClickListener() {
		public void onClick(View v) {
			//編集エリアに追加データを表示する。
			Intent intent = null;
			String strArea = (((Spinner)findViewById(R.id.cmdAreaAdvance)).getSelectedItem()).toString();
			
			if(strArea.equals("China")) {
				intent = new Intent( AdvanceSearchActivity.this, SearchResultActivity.class );
			} else {
				intent = new Intent( AdvanceSearchActivity.this, KakakuSearchResultActivity.class );
			}
			String searchkey = txtKeyword.getText().toString();
            CheckBox chkSelect = (CheckBox) findViewById(R.id.chkAdvanceAutoTranlation);
            if (chkSelect.isChecked()) {
              searchkey = AndroidUtils.getAutoTranslation(AdvanceSearchActivity.this, searchkey, strArea);
            }
			intent.putExtra("SEARCH_KEY", searchkey);
			intent.putExtra("SEARCH_MIN", startPrice.getText().toString());
			intent.putExtra("SEARCH_MAX", endPrice.getText().toString());
			intent.putExtra("SEARCH_ORD", (((Spinner)findViewById(R.id.Spinner02)).getSelectedItem()).toString());
			intent.putExtra("SEARCH_NO", "1");
			startActivity( intent );
		}
	};

	OnClickListener mClearListener = new OnClickListener() {
		public void onClick(View v) {
			txtKeyword.setText("");
			txtKeyword.setHint(getText(R.string.s_find_hit));
			startPrice.setText("");
			startPrice.setHint(getText(R.string.s_price_min_hit));
			endPrice.setText("");
			endPrice.setHint(getText(R.string.s_price_max_hit));
		}
	};
}